package android.twitter.clientroid;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import android.util.Log;


public class TweetReader
{
	
	/**
	 * a method to retrive a list of tweets from the users who the current user
	 * is following
	 * @return List JSON of other tweets
	 */
	public static ArrayList<JSONObject> retrieveSpecificUsersTweets(Twitter twitter)
	{
		List<Status> statuses = new ArrayList<Status>();
	    try {
			statuses = twitter.getFriendsTimeline();
		} catch (TwitterException e) {
			Log.e("Twitter", "Error retrieving tweets");
			Log.e("Twitter", e.getMessage());
		}

		return convertTimelineToJson(statuses);
	}
	
	public static ArrayList<JSONObject> retrieveSpecificTweets(Twitter twitter, String word)
	{
		List<Tweet> tweets = new ArrayList<Tweet>();
	    try
	    {
	    	Query query = new Query(word);
	    	query.setPage(1);
			QueryResult result = twitter.search(query);
			tweets = result.getTweets();
			
		} catch (TwitterException e) {
			Log.e("Twitter", "Error retrieving tweets");
			Log.e("Twitter", e.getMessage());
		}

		return convertTweetsToJson(tweets);
	}
	
	
	
	/**
	 * Method that converts a list of Status' to a JSON array that can
	 * be displayed by the grid view
	 * 
	 * @param statuses
	 * @return
	 */
	private static ArrayList<JSONObject> convertTimelineToJson(List<Status> statuses) {
		ArrayList<JSONObject> JOBS = new ArrayList<JSONObject>();
		try {
			if (statuses.size()>0){
				for (Status s : statuses){
					String avatar = "http://" + s.getUser().getProfileImageURL().getHost() + s.getUser().getProfileImageURL().getPath();
					JSONObject object = new JSONObject();
					object.put("tweet", s.getText());
					String timePosted = Utility.getDateDifference(s.getCreatedAt());
					object.put("tweetDate", timePosted);
					object.put("author", s.getUser().getName());
					object.put("avatar", avatar);
					object.put("userObj", s.getUser());
					object.put("tweetId", s.getId());
					
					JOBS.add(object);	
				}
			}
			else
			{
				JSONObject object = new JSONObject();
				object.put("tweet", "You have not logged in yet! Please log on to view latest tweets");
				object.put("author", "");
				JOBS.add(object);
			}

		}
		catch (JSONException e1)
		{
			Log.e("JSON", "There was an error creating the JSONObject", e1);
		}
		return JOBS;
	}
	
	private static ArrayList<JSONObject> convertTweetsToJson(List<Tweet> tweets)
	{
		ArrayList<JSONObject> JOBS = new ArrayList<JSONObject>();
		try
		{
			if (tweets.size()>0)
			{
				for (Tweet tweet : tweets)
				{
					System.out.println(tweet.getProfileImageUrl() + " , " + tweet.getFromUser() + " : " + tweet.getCreatedAt().toGMTString() + " : " + tweet.getText());
					
					String avatar = tweet.getProfileImageUrl();
					JSONObject object = new JSONObject();
					object.put("tweet", tweet.getText());
					String timePosted = Utility.getDateDifference(tweet.getCreatedAt());
					object.put("tweetDate", timePosted);
					object.put("author", tweet.getFromUser());
					object.put("avatar", avatar);
					//object.put("userObj", s.getUser());
					object.put("tweetId", tweet.getId());
					
					JOBS.add(object);
						
				}
			}
			else
			{
				JSONObject object = new JSONObject();
				object.put("tweet", "You have not logged in yet! Please log on to view latest tweets");
				object.put("author", "");
				JOBS.add(object);
			}

		}
		catch (JSONException e1)
		{
			Log.e("JSON", "There was an error creating the JSONObject", e1);
		}
		return JOBS;
	}
}
